#include "bsp.h"

#include "SW_I2C/sw_i2c.h"
#include "SW_I2C/sw_i2c_ex.h"


/* 定义IO = 1和 0的代码  */
#define I2C_SDA_1()		MPU6050_SDA_PORT->BSRR = MPU6050_SDA_PIN
#define I2C_SDA_0()		MPU6050_SDA_PORT->BSRR = MPU6050_SDA_PIN << 16;

#define I2C_SCL_1()		MPU6050_SCL_PORT->BSRR = MPU6050_SCL_PIN
#define I2C_SCL_0()		MPU6050_SCL_PORT->BSRR = MPU6050_SCL_PIN << 16;

#define I2C_SDA_READ()  ((MPU6050_SDA_PORT->IDR & MPU6050_SDA_PIN) != 0)	/* 读SDA口线状态 */
#define I2C_SCL_READ()  ((MPU6050_SCL_PORT->IDR & MPU6050_SCL_PIN) != 0)	/* 读SCL口线状态 */

#define I2C_WR	0		/* 写控制bit */
#define I2C_RD	1		/* 读控制bit */

// I2C总线时序接口函数
static void I2C_SDA_LOW(void);
static void I2C_SDA_HIGH(void);
static void I2C_SCL_LOW(void);
static void I2C_SCL_HIGH(void);
static uint8_t I2C_SDA_READ_LEVEL(void);
static void I2C_SDA_SET_INPUT(void);
static void I2C_SDA_SET_OUTPUT(void);
static void I2C_Delay(uint32_t us);

/* Private variables ---------------------------------------------------------*/
// 定义i2c驱动对象
static sw_i2c_dev_t i2c_mpu6050_dev;

void mpu6050_gpio_config(void)
{

}


void mpu6050_i2c_config(void)
{
    i2c_mpu6050_dev.name               = "i2c_mpu6050";
    i2c_mpu6050_dev.speed              = 2; /*! speed:105Hz */
    i2c_mpu6050_dev.delay_us           = I2C_Delay;
    i2c_mpu6050_dev.ops.sda_low        = I2C_SDA_LOW;
    i2c_mpu6050_dev.ops.sda_high       = I2C_SDA_HIGH;
    i2c_mpu6050_dev.ops.scl_low        = I2C_SCL_LOW;
    i2c_mpu6050_dev.ops.scl_high       = I2C_SCL_HIGH;
    i2c_mpu6050_dev.ops.sda_read_level = I2C_SDA_READ_LEVEL;
    i2c_mpu6050_dev.ops.sda_set_input  = I2C_SDA_SET_INPUT;
    i2c_mpu6050_dev.ops.sda_set_output = I2C_SDA_SET_OUTPUT;
    sw_i2c_init(&i2c_mpu6050_dev);    
}



static void mpu6050_read_regs(uint8_t reg_addr, uint8_t *data, uint8_t len)
{
    sw_i2c_ex_read_bytes(&i2c_mpu6050_dev, MPU6050_I2C_ADDR, reg_addr, data, len);
}

static void mpu6050_write_regs(uint8_t reg_addr, uint8_t *data, uint8_t len)
{
    sw_i2c_ex_write_bytes(&i2c_mpu6050_dev, MPU6050_I2C_ADDR, reg_addr, data, len);
}

uint8_t mpu6050_i2c_write(unsigned char slave_addr, unsigned char reg_addr,
                        unsigned char length, unsigned char const *data)
{
    return sw_i2c_ex_write_bytes(&i2c_mpu6050_dev, slave_addr, reg_addr, data, length);
}

uint8_t mpu6050_i2c_read(unsigned char slave_addr, unsigned char reg_addr,
                       unsigned char length, unsigned char *data)   
{
    return sw_i2c_ex_read_bytes(&i2c_mpu6050_dev, slave_addr, reg_addr, data, length);
}


void bsp_mpu6050_init(void)
{
    // GPIO初始化
    mpu6050_gpio_config();
    // I2C初始化
    mpu6050_i2c_config();

}



/***********************************************************************/

static void I2C_SDA_LOW(void)
{
    I2C_SDA_0();
}

static void I2C_SDA_HIGH(void)
{
    I2C_SDA_1();
}

static void I2C_SCL_LOW(void)
{
    I2C_SCL_0();
}

static void I2C_SCL_HIGH(void)
{
    I2C_SCL_1();
}

static uint8_t I2C_SDA_READ_LEVEL(void)
{
    return I2C_SDA_READ();
}

static void I2C_SDA_SET_INPUT(void)
{
    I2C_SDA_1(); // 释放SDA线
}

static void I2C_SDA_SET_OUTPUT(void)
{
}

static void I2C_Delay(uint32_t us)
{
    HAL_Delay_us(us);
}
